package com.prs.crm.action.cdxh;

import java.io.File;
import java.sql.Date;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;


import com.opensymphony.xwork2.ActionContext;
import com.prs.crm.action.ReportAction;
import com.prs.crm.domain.cdxh.Cdstock;
import com.prs.crm.domain.cdxh.CrmCustomer;
import com.prs.crm.domain.cdxh.DistributionOrderDetail;
import com.prs.crm.domain.cdxh.Reimbursement;
import com.prs.crm.domain.cdxh.SaleOrderDetail;
import com.prs.crm.domain.cdxh.StockPrecautionary;
import com.prs.crm.domain.cdxh.cd_Product;
import com.prs.crm.service.cdxh.CdstockService;
import com.prs.crm.service.cdxh.CrmCustomerService;
import com.prs.crm.service.cdxh.DistributionOrderDetailService;
import com.prs.crm.service.cdxh.ReimbursementService;
import com.prs.crm.service.cdxh.SaleOrderService;
import com.prs.crm.service.cdxh.StockPrecautionaryService;
import com.prs.crm.service.cdxh.cd_ProductService;

public class DistributionOrderDetailAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private PaginationSupport pagination;
	
	private  DistributionOrderDetail  distributionOrderDetail;
	private  DistributionOrderDetailService distributionOrderDetailService;
	private CrmCustomerService crmCustomerService;
	private cd_Product cdProduct;
	private cd_ProductService cd_ProductService;
	private Integer[] disIds;
	private File upload;
    private List<DistributionOrderDetail> details;
	private String uploadFileName;
	private CdstockService cdstockService;
	private StockPrecautionaryService stockPrecautionaryService;
	private Reimbursement reimbursement;
	private ReimbursementService reimbursementService;
	private SaleOrderService saleOrderService;
	
	@Action("listDistributionDetail")
	public String listDistributionDetail() {
		if(cdProduct!=null){
	    this.getDistributionOrderDetailService().saveStockNums(cdProduct);
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				cd_Product.class);
		criteria.add(Restrictions.not(Expression.eq("stockNums", 0)));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDistributionOrderDetailService().listByPage(
				criteria, pagination));
		return SUCCESS;
	}
	@Action("listDistributionDetailUpdate")
	public String listDistributionDetailUpdate() {
		
	    this.getDistributionOrderDetailService().saveStockNums(cdProduct);
		
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				cd_Product.class);
		criteria.addOrder(Order.desc("recordTime"));
		//criteria.add(Restrictions.not(Expression.eq("stockNums", 0)));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDistributionOrderDetailService().listByPage(
				criteria, pagination));
		return SUCCESS;
	}
	@Action("listDistributionDetailStockNums")
	public String listDistributionDetailStockNums() {
		
	    this.getDistributionOrderDetailService().saveStockNums(cdProduct);

		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Reimbursement.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDistributionOrderDetailService().listByPage(
				criteria, pagination));
		return SUCCESS;
	}
	@Action("viewreimbursement")
	public String viewreimbursement(){
		
		return SUCCESS;
	}
	
	@Action(value = "updateDetail", results = {
			@Result(name = "success", type = "redirect", location = "listDistributionDetailUpdate"),
			@Result(name = "error", type = "chain", location = "editDistributionDetail") })
	public String updateDetail() {
		if(reimbursement.getReimbursement()>0){
			int stockNum=cdProduct.getStockNums()-reimbursement.getReimbursement();
			//报销库存数量
			cdProduct.setStockNums(stockNum);
			this.cd_ProductService.submit(cdProduct);
			//添加报销查询
			reimbursement.setCdProduct(cdProduct);
			reimbursement.setReimDate(new java.util.Date());
			this.reimbursementService.save(reimbursement);
			/**
			 * 对库房预警进行设置
			 */
			Cdstock stockObject = this.getCdstockService().getCdstock(
					cdProduct.getProductcode(), this.cdProduct.getName());
			
			if (stockObject != null) {
				int max_Nums = stockObject.getMax_amount();
				int min_Nums = stockObject.getMin_amount();
				int amount = cdProduct.getStockNums();
	
				if (amount <= max_Nums && amount >= min_Nums) {
	
					stockObject.setAmount(cdProduct.getStockNums());
					this.getCdstockService().saveOrStock(stockObject);
				} else {
					StockPrecautionary spobject = this.getStockPrecautionaryService().getStockPrecautionaryByProductId(cdProduct.getProductcode());
					if (spobject != null) {
						spobject.setProductNum(cdProduct.getStockNums());
						this.getStockPrecautionaryService().saveOrStockPrecautionary(spobject);
					} else {
						StockPrecautionary spkk = new StockPrecautionary();
						spkk.setProductCode(cdProduct.getProductcode());
						spkk.setProductDownline(stockObject.getMin_amount());
						spkk.setProductUpline(stockObject.getMax_amount());
						spkk.setProductNum(amount);
						spkk.setProductName(cdProduct.getName()); //产品名称
						if(cdProduct.getCustomer_number()!=null){
							cd_Product cd=this.cd_ProductService.getProductObject(cdProduct.getProductcode());
							CrmCustomer crm=this.crmCustomerService.getCrmCustomerBycusCode(cd.getCustomer_number());
							spkk.setCrmcustomer(crm);        //客户
						}else{
							SaleOrderDetail dd=this.saleOrderService.getSaleOrderDetailByProduct(cdProduct);
							if(dd!=null){
								CrmCustomer crm=this.crmCustomerService.getCrmCustomerBycusCode(dd.getSaleOrder().getCusCode());
								spkk.setCrmcustomer(crm);        //客户
							}
						}
						spkk.setPrTitle("安全库存");
						this.getStockPrecautionaryService().save(spkk);
						
					}
					stockObject.setAmount(cdProduct.getStockNums());
					this.getCdstockService().saveOrStock(stockObject);
				}
			}
			
		}
			this.setPrompt("库存数量维护成功!");
		return SUCCESS;
	}
	
	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getDisIds() {
		return disIds;
	}

	public void setDisIds(Integer[] disIds) {
		this.disIds = disIds;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public DistributionOrderDetail getDistributionOrderDetail() {
		return distributionOrderDetail;
	}


	public void setDistributionOrderDetail(
			DistributionOrderDetail distributionOrderDetail) {
		this.distributionOrderDetail = distributionOrderDetail;
	}


	public DistributionOrderDetailService getDistributionOrderDetailService() {
		return distributionOrderDetailService;
	}

	public void setDistributionOrderDetailService(
			DistributionOrderDetailService distributionOrderDetailService) {
		this.distributionOrderDetailService = distributionOrderDetailService;
	}

	public cd_Product getCdProduct() {
		return cdProduct;
	}


	public void setCdProduct(cd_Product cdProduct) {
		this.cdProduct = cdProduct;
	}


	public cd_ProductService getCd_ProductService() {
		return cd_ProductService;
	}

	public void setCd_ProductService(cd_ProductService cd_ProductService) {
		this.cd_ProductService = cd_ProductService;
	}

	public List<DistributionOrderDetail> getDetails() {
		return details;
	}


	public void setDetails(List<DistributionOrderDetail> details) {
		this.details = details;
	}

	public StockPrecautionaryService getStockPrecautionaryService() {
		return stockPrecautionaryService;
	}
	public void setStockPrecautionaryService(
			StockPrecautionaryService stockPrecautionaryService) {
		this.stockPrecautionaryService = stockPrecautionaryService;
	}
	public CdstockService getCdstockService() {
		return cdstockService;
	}
	public void setCdstockService(CdstockService cdstockService) {
		this.cdstockService = cdstockService;
	}
	public CrmCustomerService getCrmCustomerService() {
		return crmCustomerService;
	}
	public void setCrmCustomerService(CrmCustomerService crmCustomerService) {
		this.crmCustomerService = crmCustomerService;
	}
	public Reimbursement getReimbursement() {
		return reimbursement;
	}
	public void setReimbursement(Reimbursement reimbursement) {
		this.reimbursement = reimbursement;
	}
	public ReimbursementService getReimbursementService() {
		return reimbursementService;
	}
	public void setReimbursementService(ReimbursementService reimbursementService) {
		this.reimbursementService = reimbursementService;
	}
	public SaleOrderService getSaleOrderService() {
		return saleOrderService;
	}
	public void setSaleOrderService(SaleOrderService saleOrderService) {
		this.saleOrderService = saleOrderService;
	}

}
